package com.xkw.autocode.code;

import java.util.List;

import com.xkw.autocode.jdbc.entity.ColumnEntity;

/**
 * html 模板生成
 * 
 * @author xiangkaiwei
 *
 */
public class VueTemplateUtils {

	private VueTemplateUtils() {

	}

	/**
	 * 查询条件
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="6">
            <el-form-item prop="beginAddress" label="装货地:">
              <el-input v-model="formData.beginAddress" />
            </el-form-item>
          </el-col>
	 * @param columns
	 * @return
	 */
	public static String getConditions(List<ColumnEntity> columns) {
		StringBuilder thSbl = new StringBuilder();

		for (ColumnEntity column : columns) {
			if (column.getColumnName().equalsIgnoreCase("id") || column.getColumnName().equalsIgnoreCase("version")) {
				continue;
			}

			 // 注释
			String desc = getDesc(column.getComments());
			// 属性名
			String attrName = CodeUtils.getAttrName(column.getColumnName());
			
			if(attrName.endsWith("Time")) {
				thSbl.append("          <el-col :xs=\"24\" :sm=\"12\" :md=\"12\" :lg=\"8\" :xl=\"6\">").append("\n");
				thSbl.append("            <el-form-item prop=\""+attrName+"\" label=\""+desc+":\">").append("\n");
				thSbl.append("              <el-date-picker").append("\n");
				thSbl.append("                v-model=\"formData."+attrName+"\"").append("\n");
				thSbl.append("                type=\"daterange\"").append("\n");
				thSbl.append("                start-placeholder=\"开始日期\"").append("\n");
				thSbl.append("                end-placeholder=\"结束日期\"").append("\n");
				thSbl.append("                :default-time=\"['00:00:00', '23:59:59']\"").append("\n");
				thSbl.append("              />").append("\n");
				thSbl.append("            </el-form-item>").append("\n");
				thSbl.append("          </el-col>").append("\n");
			}else {
				thSbl.append("          <el-col :xs=\"24\" :sm=\"12\" :md=\"12\" :lg=\"8\" :xl=\"6\">").append("\n");
				thSbl.append("            <el-form-item prop=\""+attrName+"\" label=\""+desc+":\">").append("\n");
				thSbl.append("              <el-input v-model=\"formData."+attrName+"\" />").append("\n");
				thSbl.append("            </el-form-item>").append("\n");
				thSbl.append("          </el-col>").append("\n");
			}
			
		}
		return thSbl.toString();
	}

	// 获取表格TableThead
	public static String getTableThead(List<ColumnEntity> columns) {

		StringBuilder thSbl = new StringBuilder();
		thSbl.append("[");

		int i = 0;
		for (ColumnEntity column : columns) {
			if (column.getColumnName().equalsIgnoreCase("version")) {
				continue;
			}
			 // 注释
			String desc = getDesc(column.getComments());
			if(column.getColumnName().equalsIgnoreCase("id")) {
				desc = "序号";
			}

			

			if (i > 0) {
				thSbl.append(",");
			}
			thSbl.append("'" + desc + "'");
			
			i ++;
		}
		thSbl.append("]");
		return thSbl.toString();
	}
	
	// 获取表格TableThead
	public static String getTableColumn(List<ColumnEntity> columns) {

		StringBuilder thSbl = new StringBuilder();
		thSbl.append("\t\t\t{").append("\n");

		int i = 0;
		for (ColumnEntity column : columns) {
			
			if (column.getColumnName().equalsIgnoreCase("version")) {
				continue;
			}

			if (column.getColumnName().equalsIgnoreCase("version")) {
				continue;
			}
			 // 注释
			String desc = getDesc(column.getComments());
			if(column.getColumnName().equalsIgnoreCase("id")) {
				desc = "序号";
			}
			
			// 属性名
			String attrName = CodeUtils.getAttrName(column.getColumnName());
			
			if (i > 0) {
				thSbl.append(",").append("\n");
			}
			
			thSbl.append("\t\t\t\t'" + desc + "': '").append(attrName).append("'");
			i ++;
		}
		thSbl.append("\n").append("\t\t\t}");
		return thSbl.toString();
	}
	
	
	// 获取表格getTable
	public static String getTable(List<ColumnEntity> columns) {

		StringBuilder thSbl = new StringBuilder();
		thSbl.append("\t\t\t<el-table :data=\"formData.items\" border style=\"width: 100%\">").append("\n");

		int i = 0;
		for (ColumnEntity column : columns) {
			
			if (column.getColumnName().equalsIgnoreCase("id") || column.getColumnName().equalsIgnoreCase("version")) {
				continue;
			}
			 // 注释
			String desc = getDesc(column.getComments());
			
			// 属性名
			String attrName = CodeUtils.getAttrName(column.getColumnName());
			
			if (i > 0) {
				thSbl.append("\n");
			}
			
			thSbl.append("\t\t\t\t").append("<el-table-column prop=\""+attrName+"\" label=\""+desc+"\" />");
			i ++;
		}
		thSbl.append("\n").append("\t\t\t</el-table>");
		
		return thSbl.toString();
	}
	
	// 获取表格getTable
	public static String getFormData(List<ColumnEntity> columns) {

		StringBuilder thSbl = new StringBuilder();
		thSbl.append("\t\t\tformData: {").append("\n");

		int i = 0;
		int n = 0;
		for (ColumnEntity column : columns) {
			i ++;
			
			if (column.getColumnName().equalsIgnoreCase("id") || column.getColumnName().equalsIgnoreCase("version")) {
				continue;
			}
			 // 注释
			String desc = getDesc(column.getComments());
			
			// 属性名
			String attrName = CodeUtils.getAttrName(column.getColumnName());
			
			if (n > 0) {
				thSbl.append("\n");
			}
			
			thSbl.append("\t\t\t\t").append(attrName + ": ''");
			if(i < columns.size() - 1) {
				thSbl.append(",");
			}
			thSbl.append(" // " + desc);
			n ++;
			
		}
		thSbl.append("\n").append("\t\t\t},");
		
		return thSbl.toString();
	}
	
	private static String getDesc(String desc) {
		if(desc.indexOf(' ') != -1) {
			desc = desc.substring(0,desc.indexOf(' '));
		}
		
		if(desc.indexOf('(') != -1) {
			desc = desc.substring(0,desc.indexOf('('));
		}
		
		if(desc.indexOf('（') != -1) {
			desc = desc.substring(0,desc.indexOf('（'));
		}
		if(desc.indexOf('：') != -1) {
			desc = desc.substring(0,desc.indexOf('：'));
		}
		
		return desc;
	}
}
